package chpt02;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

public class StudentGradeManagementV2 {

    public static void main(String[] args) {
        int numStudents = 5;
        Integer[][] grades = generateRandomGrades(numStudents);
        String[] studentNames = generateStudentNames(numStudents);

        System.out.println("学生姓名: " + Arrays.toString(studentNames));
        printAllGrades(studentNames, grades);

        for (int i = 0; i < 3; i++) {
            System.out.print("科目"+i+": 平均成绩 = " + calculateAverage(grades, i));
            System.out.println(", 中位数 = " + calculateMedian(grades, i));
        }

        // 按照0号科目排序
        sortGradesBySubject(studentNames, grades, 0);
        System.out.println("排序后学生的分数:");
        printAllGrades(studentNames, grades);
    }

    public static Integer[][] generateRandomGrades(int numStudents) {
        Integer[][] grades = new Integer[numStudents][3];
        Random random = new Random();
        for (int i = 0; i < numStudents; i++) {
            for (int j = 0; j < 3; j++) {
                if (random.nextInt(5) == 0) {
                    grades[i][j] = null;
                } else {
                    grades[i][j] = random.nextInt(101); // 生成0-100的随机数
                }
            }
        }
        return grades;
    }

    public static String[] generateStudentNames(int numStudents) {
        String[] studentNames = new String[numStudents];
        for (int i = 0; i < numStudents; i++) {
            studentNames[i] = "学生" + (i + 1);
        }
        return studentNames;
    }

    public static void printAllGrades(String[] studentNames, Integer[][] grades) {
        for (int i = 0; i < studentNames.length; i++) {
            System.out.println(studentNames[i] + ": " + Arrays.toString(grades[i]));
        }
    }
    
    // 按科目计算平均成绩
    public static double calculateAverage(Integer[][] grades, int subjectIndex) {
        int sum = 0;
        int count = 0;
        for (Integer[] grade : grades) {
            if (grade[subjectIndex] != null) {
                sum += grade[subjectIndex];
                count++;
            }
        }
        return count > 0 ? (double) sum / count : 0;
    }

    // 按科目计算成绩中位数
    public static double calculateMedian(Integer[][] grades, int courseId) {
        int[] validGrades = new int[grades.length];
        int count = 0;
        for (int i = 0; i < grades.length; i++) {
            if (grades[i][courseId] != null) {
                validGrades[count] = grades[i][courseId];
                count++;
            }
        }
        if (count == 0) {
            return 0;
        }
        Arrays.sort(validGrades, 0, count);
        if (count % 2 == 0) {
            return (validGrades[count / 2 - 1] + validGrades[count / 2]) / 2.0;
        } else {
            return validGrades[count / 2];
        }
    }

    // 按指定科目的成绩进行降序排序
    public static void sortGradesBySubject(String[] courseNames, Integer[][] grades, int courseId) {
        for (int i = 0; i < grades.length - 1; i++) {
            for (int j = 0; j < grades.length - i - 1; j++) {
                if (grades[j][courseId] == null || (grades[j + 1][courseId] != null && grades[j][courseId] < grades[j + 1][courseId])) {
                    // 交换成绩
                    Integer temp = grades[j][courseId];
                    grades[j][courseId] = grades[j + 1][courseId];
                    grades[j + 1][courseId] = temp;
                    // 同时也要交换学生姓名
                    String tempName = courseNames[j];
                    courseNames[j] = courseNames[j + 1];
                    courseNames[j + 1] = tempName;
                }
            }
        }
    }
}